﻿Namespace FileSystemScan
    Friend Class Handler_GroupIndividualFiles
        Friend Shared Sub Scan(ByVal share As Entities.Share, ByVal rootPath As String)
            Handler_ScanFolder.Scan(share, AddressOf GroupFilesToItemCandidates, AddressOf GetKeyForItem, rootPath)
        End Sub

        Private Shared Function GroupFilesToItemCandidates(ByVal share As Entities.Share, ByVal files As Generic.List(Of IO.FileInfo)) As Generic.Dictionary(Of String, Handler_ScanFolder.ItemCandidate)
            Dim aRet As New Generic.Dictionary(Of String, Handler_ScanFolder.ItemCandidate)

            For Each clsFI As IO.FileInfo In files
                Dim clsCand As Handler_ScanFolder.ItemCandidate = GetItemCandidateNameByFilePath(clsFI.Name)
                clsCand.AddFile(clsFI)
                aRet.Add(clsFI.FullName, clsCand)
            Next
            Return aRet
        End Function
        Private Shared Function GetKeyForItem(ByVal share As Entities.Share, ByVal item As Entities.SharedItem) As String
            Return System.IO.Path.Combine(item.Directory, item.Name)
        End Function

        Private Shared Function GetItemCandidateNameByFilePath(ByVal filePath As String) As Handler_ScanFolder.ItemCandidate
            Dim clsRet As New Handler_ScanFolder.ItemCandidate
            'remove everything but the actual filename
            clsRet.Key = filePath

            Dim strFileName As String = System.IO.Path.GetFileNameWithoutExtension(filePath)

            With Framework.IO.Utils.CleanUpFilename(strFileName)
                strFileName = .Name
                clsRet.Comment = .Comment
            End With

            clsRet.Name = IO.Path.GetFileName(filePath)
            Return clsRet
        End Function
    End Class
End Namespace
